<?php
	class AdminAction extends Action{
		public function Index(){
			 if(session('name')==''){
			 	$this->error('非法请求，请通过身份验证请求','Login');
			 }
			 $mapData=M('user')->field('city as name,count(city) as value')->group('city')->select();
			 $mapData=json_encode($mapData);
			 $this->assign('mapData', $mapData);
			 //dump($mapData);die;
			$this->display();
		}
		public function Product(){
			if(session('name')==''){
			$this->error('非法请求，请通过身份验证请求','Login');
			}
			session('[start]');
			import('ORG.Util.Page');
			$count=M('Products')->count();
			$page=new Page($count,7);
			$limit=$page->firstRow.','.$page->listRows;
			$board=M('Products')->limit($limit)->select();
			$this->board=$board;
			$this->page=$page->show();
			
			$proTop=M('Products')->order('ProSaleCount DESC')->limit(0,10)->select();
			$saleCount=M('Products')->sum('ProSaleCount');
			$saleCount=intval($saleCount);			
			$this->assign('saleCount',$saleCount);
			$this->assign('proTop',$proTop);
			$this->display();
		}
		public function ProForm(){
			if(session('name')==''){
			$this->error('非法请求，请通过身份验证请求','Login');
			}
			session('[start]');
			$ProID=I('ProID');
			$result="";
			if($ProID!=""){
				$result=M('Products')->where(array('ProID'=>$ProID))->select();
			}
			$this->assign('rowPro',$result);
			$this->display();
		}
		public function FormHandle(){
			if(IS_POST){
				$data="";
				if($_FILES['ProImg']['name']!=""){
					$info=imgUpLoad();
					$data=array(
					'ProImg'=>$info[0]['savename'],
					'ProID'=>I('ProID','','htmlspecialchars'),
					'ProName'=>I('ProName','','htmlspecialchars'),
					'ProPrice'=>I('ProPrice','','htmlspecialchars'),
					'ProNum'=>I('ProNum','','htmlspecialchars'),
					'ProEms'=>I('ProEms','','htmlspecialchars'),
					'ProStau'=>I('ProStau','','htmlspecialchars'),
					'ProAd'=>I('ProAd','','htmlspecialchars'),
					'ProText'=>I('ProText','','htmlspecialchars'),
					'CreaTime'=>time()
					);
				}else{
					$data=array(
					'ProID'=>I('ProID','','htmlspecialchars'),
					'ProName'=>I('ProName','','htmlspecialchars'),
					'ProPrice'=>I('ProPrice','','htmlspecialchars'),
					'ProNum'=>I('ProNum','','htmlspecialchars'),
					'ProEms'=>I('ProEms','','htmlspecialchars'),
					'ProStau'=>I('ProStau','','htmlspecialchars'),
					'ProAd'=>I('ProAd','','htmlspecialchars'),
					'ProText'=>I('ProText','','htmlspecialchars'),
					'CreaTime'=>time()
					);
				}
				if($data['ProID']==""){
					$id=M('Products')->data($data)->add();
					if($id>0){
						$this->success('添加成功',U('Product','','',0,true));
					}else{
						$this->error('添加失败请重试',U('ProForm','','',0,true));
					}
				}else if($data['ProID']!=""){					
					$row=M('Products')->data($data)->save();
					if($row>0){
						$this->success('更新成功',U('Product','','',0,true));
					}else{
						$this->error('更新失败请重试',U('Product','','',0,true));
					}
				}
			}else{
				$ProID=I('ProID','','htmlspecialchars');
				$row=M('Products')->where(array('ProID'=>$ProID))->delete();
				if($row>0){
						$this->success('删除成功',U('Product','','',0,true));
					}else{
						$this->error('更新失败请重试',U('Product','','',0,true));
					}
			}
			
		}
		public function OrderList(){
			if(session('name')==''){
			$this->error('非法请求，请通过身份验证请求','Login');
			}
			session('[start]');
			$OrderState=I('OrderState');
			if($OrderState==""){
				$OrderState=2;
			}
			import('ORG.Util.Page');
			$count=M('Order')->where(array('OrderState'=>$OrderState))->count();
			$page=new Page($count,7);
			$limit=$page->firstRow.','.$page->listRows;
			$board=M('Order')->where(array('OrderState'=>$OrderState))->limit($limit)->select();
			$this->board=$board;
			$this->page=$page->show(); 
			$this->display();
		}
		public function OrderHandle(){
			if(IS_POST){
				$data=array(
					'OrderId'=>I('OrderId','','htmlspecialchars'),
					'UserName'=>I('UserName','','htmlspecialchars'),
					'UserPhone'=>I('UserPhone','','htmlspecialchars'),
					'Address'=>I('Address','','htmlspecialchars'),
					'Total'=>I('Total','','htmlspecialchars'),
					'Demand'=>I('Demand','','htmlspecialchars'),					
					);					
				$row=M('Order')->data($data)->save();
				if($row>0){
					$this->success('更新成功',U('OrderList','','',0,true));
				}else{
					$this->error('更新失败请重试',U('OrderList','','',0,true));
				}
			}else{
				$OrderId=I('OrderId','','htmlspecialchars');
				$OrderState=I('OrderState','','htmlspecialchars');
				if($OrderState==""){
					$row=M('Order')->where(array('OrderId'=>$OrderId))->delete();
					if($row>0){
						$this->success('删除成功',U('OrderList','','',0,true));
					}else{
						$this->error('删除失败请重试',U('OrderList','','',0,true));
					}
				}else{
					$data=array(
						'OrderId'=>$OrderId,
						'OrderState'=>$OrderState,
						'sendDate'=>time()
					);
					$row=M('Order')->data($data)->save();
					if($row>0){
						$order=M('Order')->where(array('OrderId'=>$OrderId))->select();
						$User=M('User')->where(array('UserId'=>$order[0]['UserId']))->select();
						order_send_message($User[0]['openid'],$order[0]['Total'],"aaaa",$order[0]['Address'],$order[0]['OrderId']);
						$this->success('更新状态成功',U('OrderList','','',0,true));
					}else{
						$this->error('更新状态失败请重试',U('OrderList','','',0,true));
					}
				}
				
			}
		}
		public function OrdForm(){
			if(session('name')==''){
			$this->error('非法请求，请通过身份验证请求','Login');
			}
			session('[start]');
			$OrderId=I('OrderId');
			$result="";
			if($OrderId==""){
				$this->error('未选择任何数据',U('OrderList','','',0,true))	;
			}
			$result=M('Order')->where(array('OrderId'=>$OrderId))->select();
			$this->assign('rowOrd',$result);
			$this->display();
		}
		public function Member(){
			if(session('name')==''){
			$this->error('非法请求，请通过身份验证请求','Login');
			}
			session('[start]');
			import('ORG.Util.Page');
			$count=M('Member')->count();
			$page=new Page($count,7);
			$limit=$page->firstRow.','.$page->listRows;
			$board=M('Member')->limit($limit)->select();
			$this->board=$board;
			$this->page=$page->show(); 
			$this->display();
		}
		public function MemForm(){
			if(session('name')==''){
			$this->error('非法请求，请通过身份验证请求','Login');
			}
			session('[start]');
			$arr=split('#',session('name'));
			$this->assign('arr',$arr);
			$MemID=I('MemID');
			$result="";
			$result=M('Member')->where(array('MemID'=>$MemID))->select();
			$this->assign('rowMem',$result);
			$this->display();
		}
		public function MemHandle(){
			if(IS_POST){
				$PassWord=I('PassWord','','htmlspecialchars');
				if(strlen($PassWord)==32){
					$data=array(
					'MemID'=>I('MemID','','htmlspecialchars'),
					'MemName'=>I('MemName','','htmlspecialchars'),
					'LoginName'=>I('LoginName','','htmlspecialchars'),
					'Status'=>I('Status','','htmlspecialchars'),					
					);	
				}else{
					$data=array(
					'MemID'=>I('MemID','','htmlspecialchars'),
					'MemName'=>I('MemName','','htmlspecialchars'),
					'LoginName'=>I('LoginName','','htmlspecialchars'),
					'PassWord'=>MD5($PassWord),
					'Status'=>I('Status','','htmlspecialchars'),					
					);	
				}
				if($data['MemID']==""){
					$row=M('Member')->data($data)->add();
					if($row>0){
						$this->success('数据添加成功',U('Member','','',0,true));
					}else{
						$this->error('数据添加失败',U('Member','','',0,true));
					}
				}else{
					$row=M('Member')->data($data)->save();
					if($row>0){
						$this->success('更新成功',U('Member','','',0,true));
					}else{
						$this->error('更新失败请重试',U('Member','','',0,true));
					}
				}				
				
			}else{
				$MemID=I('MemID','','htmlspecialchars');
				$row=M('Member')->where(array('MemID'=>$MemID))->delete();
				if($row>0){
					$this->success('删除成功',U('Member','','',0,true));
				}else{
					$this->error('删除失败请重试',U('Member','','',0,true));
				}
			}
		}
		public function Login(){
			if(IS_POST){
				$userName=I('username','','htmlspecialchars');
				$passWord=MD5(I('password','','htmlspecialchars'));
				$row=M('Member')->where(array('LoginName'=>$userName))->select();
				if($row[0]['LoginName']!=""){
					if($row[0]['PassWord']==$passWord && $row[0]['Status']!=-1){
						session('name',$row[0]['MemName']."#".$row[0]['Status']."#".$row[0]['MemID']);
						$this->success($row[0]['MemName'].',欢迎您^_^',U('Index','','',0,true));
					}else{
						session('[destroy]');
						$this->error('您输入的密码不正确或帐号不可用,请重试:(',U('Login','','',0,true));
					}
				}else{
					session('[destroy]');
					$this->error('您输入的用户名不存在,请重试:(',U('Login','','',0,true));
				}
			}else{
				session('[destroy]');
				//$system=sys_windows();
				//$this->assign('system',$system);
				$this->display();
			}
		}
		public function Person(){
			$arr=split('#',session('name'));
			$data=M('Member')->where(array('MemID'=>$arr[2]))->select();
			$this->assign('data',$data);
			$this->display();
		}
		public function PerHandle(){
			$newPass=I('newPass','','htmlspecialchars');
			$oldPass=I('oldPass','','htmlspecialchars');
			$MemID=I('MemID','','htmlspecialchars');
			if($oldPass==''){
				$data=array(
					'MemID'=>$MemID,
					'MemName'=>I('MemName','','htmlspecialchars'),
					'LoginName'=>I('LoginName','','htmlspecialchars'),				
					);
				$row=M('Member')->data($data)->save();
				if($row>0){
					$this->success('个人信息更新成功',U('Login','','',0,true));
				}else{
					$this->error('个人信息更新失败请重试',U('Person','','',0,true));
				}
			}else{
				$row=M('Member')->where(array('MemID'=>$MemID))->select();
				if($row[0]['PassWord']==MD5($oldPass)){
					$date=array(
					'MemID'=>$MemID,
					'LoginName'=>I('LoginName','','htmlspecialchars'),
					'PassWord'=>MD5($newPass),	
					);
					$id=M('Member')->data($date)->save();
					if($row>0){
						$this->success('密码更新成功',U('Login','','',0,true));
					}else{
						$this->error('密码更新失败请重试',U('Person','','',0,true));
					}
				}else{
					$this->error('原密码验证失败，请重试',U('Person','','',0,true));
				}
			}
		}
		public function Repass(){
			$arr=split('#',session('name'));
			$data=M('Member')->where(array('MemID'=>$arr[2]))->select();
			$this->assign('data',$data);
			$this->display();
		}
		public function info(){
			$data=array(
				'touser'=>"ojM2hsxavDmUtLhRN-Y3am41w1ZY",
				'template_id'=>"TMOt7oF6nNZRjyxXF7KOlEVtSaAyf98nBly8ydJOtYI",
				'url'=>'http://weixin.qq.com/download',
				'data'=>array(
					'first'=>array('value'=>urlencode("您的订单已提交成功"),'color'=>"#173177"),
					'orderID'=>array('value'=>"2016042301",'color'=>"#173177"),
					'orderMoneySum'=>array('value'=>urlencode("30.00元"),'color'=>"#173177"),
					'backupFieldName'=>array('value'=>urlencode("商品信息:")),
					'backupFieldData'=>array('value'=>urlencode("移动存储")),
					'remark'=>array('value'=>urlencode("如有问题请致电400-828-1878或直接在微信留言，小易将第一时间为您服务!"))
				)	
			);
			$result=send_template_message(urldecode(json_encode($data)));
			dump($result);
		}
	}
?>